/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.source.linkedin;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author magzupao
 */
public class ProcesaXML {

    private String clave;
    private String valor;
    private Map<String, String> mapDatos;
    private Map<Integer, String> mapExperiencia;
    private Map<Integer, String> mapEducacion;
    private Map<Integer, String> mapCertificacion;
    private List listaExperiencia = new ArrayList();
    private List listaEducacion = new ArrayList();
    private List listaCertificacion = new ArrayList();
    private int opcion = 0;
    private int contador = 0;

    public String getClave() {
        return clave;
    }

    public void setClave(String clave) {
        this.clave = clave;
    }

    public String getValor() {
        return valor;
    }

    public void setValor(String valor) {
        this.valor = valor;
    }

    public Map<String, String> getMapDatos() {
        return mapDatos;
    }

    public void setMapDatos(Map<String, String> mapDatos) {
        this.mapDatos = mapDatos;
    }

    public Map<Integer, String> getMapEducacion() {
        return mapEducacion;
    }

    public void setMapEducacion(Map<Integer, String> mapEducacion) {
        this.mapEducacion = mapEducacion;
    }

    public Map<Integer, String> getMapExperiencia() {
        return mapExperiencia;
    }

    public void setMapExperiencia(Map<Integer, String> mapExperiencia) {
        this.mapExperiencia = mapExperiencia;
    }

    public Map<Integer, String> getMapCertificacion() {
        return mapCertificacion;
    }

    public void setMapCertificacion(Map<Integer, String> mapCertificacion) {
        this.mapCertificacion = mapCertificacion;
    }

    public List getListaExperiencia() {
        return listaExperiencia;
    }

    public void setListaExperiencia(List listaExperiencia) {
        this.listaExperiencia = listaExperiencia;
    }

    public List getListaEducacion() {
        return listaEducacion;
    }

    public void setListaEducacion(List listaEducacion) {
        this.listaEducacion = listaEducacion;
    }

    public List getListaCertificacion() {
        return listaCertificacion;
    }

    public void setListaCertificacion(List listaCertificacion) {
        this.listaCertificacion = listaCertificacion;
    }

    public ProcesaXML() {
    }

    public void procesa(String cadena) throws Exception {

        XMLInputFactory factory = XMLInputFactory.newInstance();
        InputStream inputStream = new ByteArrayInputStream(cadena.getBytes("UTF-8"));
        Reader filereader = new InputStreamReader(inputStream, "UTF-8");
        XMLEventReader reader = factory.createXMLEventReader(filereader);
        contador = 0;
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                StartElement element = (StartElement) event;
                
                if (element.getName().toString().equals("datos_personales")) {
                    opcion = 1;
                    mapDatos = new HashMap<String, String>();
                }
                if (element.getName().toString().equals("position")) {
                    opcion = 2;
                    mapExperiencia = new HashMap<Integer, String>();
                }
                if (element.getName().toString().equals("educacion")) {
                    opcion = 3;
                    mapEducacion = new HashMap<Integer, String>();
                }
                if (element.getName().toString().equals("certificacion")) {
                    opcion = 4;
                    mapCertificacion = new HashMap<Integer, String>();
                }
                Iterator iterator = element.getAttributes();
                while (iterator.hasNext()) {
                    Attribute attribute = (Attribute) iterator.next();
                    QName name = attribute.getName();
                    if (name.toString().equals("valor")) {
                        setValor(attribute.getValue().toString());                        
                    }
                    if (name.toString().equals("name")) {
                        setClave(attribute.getValue().toString());
                        cargaDatos();
                    }
                }
            }
            if (event.isEndElement()) {
                EndElement element = (EndElement) event;
                
            }
            if (event.isCharacters()) {
                Characters characters = (Characters) event;
                
            }
        }
    }

    public void cargaDatos() {
        switch (opcion) {
            case 1:
                mapDatos.put(getClave(), getValor());
                break;
            case 2:
                mapExperiencia.put(devuelvePosicion(getClave()), getValor());
                if(mapExperiencia.size()==4){
                    listaExperiencia.add(mapExperiencia);
                }
                break;
            case 3:
                mapEducacion.put(devuelvePosicion(getClave()), getValor());
                if (mapEducacion.size() == 4) {
                    listaEducacion.add(mapEducacion);
                }
                break;
            case 4:
                mapCertificacion.put(contador, getValor());
                contador++;
                listaCertificacion.add(mapCertificacion);
                break;
            default:
                ;
                break;
        }
        ;
    }

    public int devuelvePosicion(String cadena) {
        int valor = 10;
        
        if (cadena.equals("cargo")) {
          
            valor = 0;
        }
        if (cadena.equals("empresa")) {
            
            valor = 1;
        }
        if (cadena.equals("periodo")) {
            
            valor = 2;
        }
        if (cadena.equals("funciones")) {
           
            valor = 3;
        }

        if (cadena.equals("intsuni")) {
            valor = 0;
        }
        if (cadena.equals("titulo")) {
            valor = 1;
        }
        if (cadena.equals("estudio")) {
            valor = 2;
        }        
        if (cadena.equals("etapa")) {
            valor = 3;
        }

        return valor;
    }
}
